#include "spazbot.bi"
''#include "../sock.bi"

dim as string inkeyString, MOTD, nick, chanrecv, nicktosend

nick = "RedBall"

dim as sock.IRC mySock = sock.IRC( "irc.freenode.net", nick, nick )

dim as IRC.message myMessage

dim as ubyte reloadauth, allowauth, denyauth

do 
  
  if mySock.getLine( MOTD ) then
  
    if left(MOTD,4)="PING" then MySock.PutLine( "PONG" & ( right ( MOTD, len( MOTD ) -4 )))

    if myMessage.parse( MOTD ) then
      
      dim as integer iParm = ANY
      for iParm = 1 to myMessage.params - 1
        
        ? myMessage.param( iParm ); " ";
	
      next
      ?
  
    end if 
  
   end if
  
   motd = myMessage.param( myMessage.trailing )

loop until motd = "MOTD File is missing" or motd = "End of /MOTD command."


if mySock.isClosed( ) then

  ? mySock.getError( )
  
else

  dim as string thisMessage
  
  ? "RedBall started"
  ? 
  ? "hit 'b' to test bold text"
  ? "hit 'v' to test inverted text"
  ? "hit 'u' to test underlined text"
  ? "hit 'c' to test colorful text"
  ? "hit 'n' to change nickname"
  ? "hit 'j' to join a channel"
  ? "hit 't' to talk! (Sends to #gurumeditation only)"
  ? "hit 'p' to part (leave) a channel"
  ? "hit 'r' to send rainbow text."
  ? "hit 'w' to send a raw message."
  ? "hit 'q' to quit the server and application."
  
  
  do
  
    inkeyString = inkey

    /'  Did we get a message? '/
    if mySock.getLine( thisMessage ) then
    
    myMessage.parse( thisMessage )  
 
    dim n as integer
    n=isAllowed(ltrim(rtrim(myMessage.id)))
   
    ''Send it to the plugins if the guy who sent it was authed...
    if n then
	dim msg as message
	for i as integer = 0 to numofplugins
		msg=botarray(i).BotMain(thisMessage)
		if msg.msgtype=1 then
			mySock.sendMessage( msg.dest, msg.msg )
		elseif msg.msgtype=2 then
			mySock.sendNotice ( msg.dest, msg.msg)
		elseif msg.msgtype=3 then
			mySock.PutLine( "MODE " & msg.dest & " :" & msg.msg )
		elseif msg.msgtype=4 then
			mySock.PutLine ( msg.msg )
		endif
	next
    endif

     if left(thisMessage,4)="PING" then MySock.PutLine( "PONG" & ( right ( thisMessage, len( thisMessage ) -4 )))      

      with myMessage
        
        /' Somebody messaged, and we can see it. '/
        if .command = "PRIVMSG" then
          
          /' Was that message only for us? '/
          if .param( 0 ) = "bot" Then
            
            ? "(pm) ";
          
          end if
          
          /' Who said it: What did they say? '/
          ? "<" & .id & "> " & .param( .trailing )
          
        end if
        
      end with
      
    end if

    if instr(thisMessage, "^allow") then
      if myMessage.id = "_spaz" or myMessage.id = "King_anarky" then
        mySock.putLine ("WHOIS :" & myMessage.id)
	allowauth=1
	nicktosend=right(myMessage.param(myMessage.trailing), len(myMessage.param(myMessage.trailing))-7)
      endif
   endif
   
   if allowauth=1 and instr(thisMessage, "is identified to services") then
	AddNick(nicktosend)
        allowauth=0
   endif
   
   if instr(thisMessage, "^remove") then
      if myMessage.id = "_spaz" or myMessage.id = "King_anarky" then
        mySock.putLine ("WHOIS :" & myMessage.id)
	denyauth=1
	nicktosend=right(myMessage.param(myMessage.trailing), len(myMessage.param(myMessage.trailing))-8)
      endif
   endif
   
   if denyauth=1 and instr(thisMessage, "is identified to services") then
	delNick(nicktosend)
        denyauth=0
   endif

    if inkeyString = " " or instr(thisMessage, "!reload") then 
     
      if myMessage.id = "_spaz" or myMessage.id = "King_anarky" then
        mySock.putLine ("WHOIS :" & myMessage.id)
	reloadauth=1
      endif

      chanrecv=myMessage.param(0)
    
    endif

    if reloadauth=1 and instr(thisMessage, "is identified to services") then
      mySock.sendMessage( chanrecv, "Reloading plugins..." )
      dim oldtime as double
      oldtime=timer

      for i as integer = 0 to numofplugins
           dylibfree(BotArray(i).hndl)
      next

      LoadPlugins ()

      mySock.sendMessage( chanrecv, "Reloaded all plugins in " & (timer - oldtime)*1000 & " milliseconds" )
    
      reloadauth=0

    endif

    if inkeyString = "b" then
      
      mySock.sendMessage( "", IRC.bold( "bold" ) & "." )
      
    end if
      
    if inkeyString = "v" then
    
      mySock.sendMessage( "", IRC.invert( "inverted" ) & "." )
      
    end if
      
    if inkeyString = "w" then
    
      dim as string rawMsg
      line input "Message: ", rawMsg

      mySock.putLine( rawMsg )
      
    end if
      
    if inkeyString = "u" then
    
      mySock.sendMessage( "", IRC.underline( "underline" ) & "." )
      
    end if
      
    if inkeyString = "c" then
    
      mySock.sendMessage( "", IRC.color( "testing", IRC.green, IRC.purple ) & " " & IRC.color( "multiple", IRC.Cyan, IRC.brown ) & " " & IRC.color( "calls", IRC.white, IRC.Green ) & "." )
      
    end if
      
    if inkeyString = "s" then
    
      mySock.sendMessage( "", IRC.color( "testing " & IRC.bold( "nested" ) & " statements", 4, 2 ) )
      
    end if
      
    if inkeyString = "m" then
    
      mySock.sendMessage( "", chr(2) & "testing" & chr(22) & " malformed " & chr(2) & "nested" & chr(22) )
      
    end if
      
    if inkeyString = "r" then
    
        dim as string thing, chan
	line input "Message: ", thing
	input "channel: ", chan
	mySock.sendMessage( chan, IRC.rainbow( thing ) )
      
    end if
      
    if inkeyString = "q" then
    
      dim as string quitmsg
      input "quit message: ",quitmsg
      
      for i as integer = 0 to numofplugins
	   dylibfree(BotArray(i).hndl)
      next
           
      mySock.quit( quitmsg )
      
    end if
    
    if inkeyString = "n" then
    
      dim as string toNick
      line input "To nick: ", toNick

      mySock.changeNick( toNick )
      
    end if
    
    if inkeyString = "j" then
    
      dim as string toChan
      line input "To channel: ", toChan
    
      mySock.joinChannel( toChan )
      
    end if

    if inkeyString = "t" then
    
      dim as string toChan, chan
      input "Channel: ", chan
      line input "Message: ", toChan
    
      mySock.sendMessage( chan, toChan )
      
    end if
    
    if inkeyString = "p" then
    
      dim as string leaveChan, leaveMsg
      line input "Part channel: ", leaveChan
      line input "Parting msg: ", leaveMsg
    
      mySock.partChannel( leaveChan, leaveMsg )

    end if
    
    /' 20 loops a second should be plenty. '/
    sleep 50, 1

    if mySock.isClosed( ) then
      
      /' connection was kill'd '/  
      exit do
      
    end if
  
  /' loop until ESCAPE is hit '/
  loop until ( inkeyString = chr( 27 ) )

end if  
